package com.lfk.justweengine.engine.Layer;

import com.lfk.justweengine.drawable.Sprite.BaseSub;
import com.lfk.justweengine.engine.Engine;
import com.lfk.justweengine.utils.logger.LogLevel;
import com.lfk.justweengine.utils.logger.Logger;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.Rect;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 使用了Layer分层的Engine
 *
 * @author liufengkai
 *         Created by liufengkai on 16/5/5.
 */
public abstract class LayerEngine extends Engine {
    protected Screen layerEngineScreen;

    protected DependentLayout layerEngineScreenLayout;

    public abstract void init();

    public abstract void load();

    public abstract void update();

    public abstract void touch(TouchEvent event);

    public abstract void collision(BaseSub baseSub);

    public LayerEngine() {
        // init logger
        Logger.init().logLevel(LogLevel.NONE);
    }

    public LayerEngine(boolean isOpenDebug) {
        this.isOpenDebug = isOpenDebug;
        if (!isOpenDebug) {
            Logger.init().logLevel(LogLevel.NONE);
        } else {
            Logger.init();
        }
    }

    private void Engine() {
        this.layerEngineScreen = new Screen(this, new Screen.ScreenListener() {
            @Override
            public void Init() {
                init();
            }

            @Override
            public void Load() {
                load();
            }

            @Override
            public void Update() {
                update();
            }

            @Override
            public void Touch(TouchEvent event) {
                touch(event);
            }
        });

        layerEngineScreenLayout = new DependentLayout(this);
        layerEngineScreenLayout.addComponent (layerEngineScreen, new StackLayout.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_PARENT
        ));
    }

    @Override
    protected void onStart(Intent savedInstanceState) {
        super.onStart(savedInstanceState);

        Logger.d("engine onStart start");

        this.Engine();
        setUIContent(layerEngineScreenLayout);
        layerEngineScreen.createScreen();

        Logger.d("engine onStart end");
    }

    /**
     * engine onActive
     */
    @Override
    protected void onActive() {
        super.onActive();
        Logger.d("engine onActive");
        layerEngineScreen.restart();
        // need add...
    }

    /**
     * engine onInactive
     */
    @Override
    protected void onInactive() {
        super.onInactive();
        Logger.d("engine onInactive");
        layerEngineScreen.pause();
        layerEngineScreen.addPauseTime();
        // need add...
    }

    /**
     * 设定整体背景
     *
     * @param color
     */
    public void setBackgroundColor(int color) {
        this.layerEngineScreen.setBackgroundColor(color);
    }

    /**
     * set screenOrientation
     *
     * @param mode
     */
    public void setScreenOrientation(ScreenMode mode) {setDisplayOrientation(mode.value);}

    public Screen getScreen() {
        return layerEngineScreen;
    }

    public void debugDraw(Rect bound) {
        layerEngineScreen.debugDraw(bound);
    }

    /**
     * 添加一层
     *
     * @param layer
     */
    public void addLayer(Layer layer) {
        layerEngineScreen.addLayer(layer);
    }

    /**
     * 在某层之上添加一层
     *
     * @param layer
     * @param name
     */
    public void addLayerBefore(Layer layer, String name) {
        layerEngineScreen.addLayerBefore(layer, name);
    }

    /**
     * 在某层之下添加一层
     *
     * @param layer
     * @param name
     */
    public void addLayerAfter(Layer layer, String name) {
        layerEngineScreen.addLayerAfter(layer, name);
    }


}
